package com.demo.java.net.datagram;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * Created by carlkong on 2016/5/26.
 */
public class TestMultiClientDatagramServer extends Thread{
    private DatagramSocket socket;
    private DatagramPacket packet;
    private byte[] data;

    public TestMultiClientDatagramServer(DatagramSocket socket, DatagramPacket packet,byte[] data) {
        this.socket = socket;
        this.packet = packet;
        this.data = data;
    }

    public void run() {
        try {
            //4、读取数据
            String info = new String(data, 0, data.length);
            System.out.println("我是服务器，客户端告诉我" + info);

            //向客户端响应数据
            //1、定义客户端的地址、端口号、数据
            InetAddress address = packet.getAddress();
            int port = packet.getPort();
            byte[] data2 = "欢迎您！".getBytes();
            //2、创建数据报，包含响应的数据信息
            DatagramPacket packet2 = new DatagramPacket(data2, data2.length, address, port);
            //3、响应客户端
            socket.send(packet2);
            //4、关闭资源
            socket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        try {
            //1、创建服务器端DatagramSocket，指定端口
            DatagramSocket socket = new DatagramSocket(10010);
            //2、创建数据报，用于接受客户端发送的数据
            byte[] data = new byte[1024];//
            DatagramPacket packet = new DatagramPacket(data, data.length);
            while (true) {
                //3、接受客户端发送的数据
                socket.receive(packet);//此方法在接受数据报之前会一致阻塞
                System.out.println("新客户端连接成功：" + socket.getInetAddress() + ":" + socket.getPort());
                TestMultiClientDatagramServer testDatagramServer = new TestMultiClientDatagramServer(socket,packet,data);
                testDatagramServer.start();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
